package com.cpfmat.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cpfmat.common.CommonStringUtils;
import com.cpfmat.common.Contains;
import com.cpfmat.entity.Article;
import com.cpfmat.entity.SystemMenu;
import com.cpfmat.service.IArticleService;
import com.cpfmat.service.ISystemMenuService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangbin
 * @since 2018-07-30
 */
@Controller
@RequestMapping("/manage/systemMenu")
public class SystemMenuController {
	@Autowired
	private ISystemMenuService systemMenuService;
	@Autowired
	private IArticleService articleService;
	
	/**
	 * 初始化菜单
	 * @return
	 */
	@RequestMapping("/init")
	@ResponseBody
	public boolean initMenu() {
		try{
			// 先拿到所有旧的key和seqId
			List<SystemMenu> allOldMenu = systemMenuService.selectList(new EntityWrapper<SystemMenu>());
			Map<String,String> oldSeqIdMap = new HashMap<String,String>();
			Iterator<SystemMenu> allOldMenuIterator = allOldMenu.iterator();
			while(allOldMenuIterator.hasNext()){
				SystemMenu article = allOldMenuIterator.next();
				oldSeqIdMap.put(article.getSeqId(), article.getKey());
			}
			// 全删
			EntityWrapper<SystemMenu> wrapper = new EntityWrapper<SystemMenu>();
			systemMenuService.delete(wrapper);
			/**
			 * 			栏目		导航		父级
			 * 首页		true	true	null
			 * 平台介绍	true	false	null
			 * 平台动态	true	true	null
			 * 信息发布	true	true	null
			 * 权威解读	true	true	null
			 */
			Date nowTime = new Date();
			String key = null;
			
			List<SystemMenu> addList = new ArrayList<SystemMenu>();
			Map<String,String> seqIdMap = new HashMap<String,String>();
			
			SystemMenu homePage = new SystemMenu();
			String uuid = CommonStringUtils.getUUID();
			key = "index";
			homePage.setSeqId(uuid);
			homePage.setShowName("首页");
			homePage.setIsColumn(1);
			homePage.setIsNav(1);
			homePage.setParentId(null);
			homePage.setInsertTime(nowTime);
			homePage.setOrderNum(0);
			homePage.setUpdateCounts(0);
			homePage.setUpdateTime(nowTime);
			homePage.setUrl("/");
			homePage.setKey(key);
			homePage.setDelFlag(0);
			addList.add(homePage);
			
			SystemMenu about = new SystemMenu();
			uuid = CommonStringUtils.getUUID();
			key = "about";
			about.setSeqId(uuid);
			about.setShowName("平台介绍");
			about.setIsColumn(1);
			about.setIsNav(0);
			about.setParentId(null);
			about.setInsertTime(nowTime);
			about.setOrderNum(1);
			about.setUpdateCounts(0);
			about.setUpdateTime(nowTime);
			about.setUrl("/" + key);
			about.setDelFlag(0);
			about.setKey(key);
			addList.add(about);
			seqIdMap.put(key, uuid);
			
			SystemMenu platform = new SystemMenu();
			uuid = CommonStringUtils.getUUID();
			key = "platform";
			platform.setSeqId(uuid);
			platform.setShowName("平台动态");
			platform.setIsColumn(1);
			platform.setIsNav(1);
			platform.setParentId(null);
			platform.setInsertTime(nowTime);
			platform.setOrderNum(2);
			platform.setUpdateCounts(0);
			platform.setUpdateTime(nowTime);
			platform.setUrl("/" + key);
			platform.setDelFlag(0);
			platform.setKey(key);
			addList.add(platform);
			seqIdMap.put(key, uuid);
			
			SystemMenu news = new SystemMenu();
			uuid = CommonStringUtils.getUUID();
			key = "news";
			news.setSeqId(uuid);
			news.setShowName("信息发布");
			news.setIsColumn(1);
			news.setIsNav(1);
			news.setParentId(null);
			news.setInsertTime(nowTime);
			news.setOrderNum(3);
			news.setUpdateCounts(0);
			news.setUpdateTime(nowTime);
			news.setUrl("/" + key);
			news.setDelFlag(0);
			news.setKey(key);
			addList.add(news);
			seqIdMap.put(key, uuid);
			
			SystemMenu authority = new SystemMenu();
			uuid = CommonStringUtils.getUUID();
			key = "authority";
			authority.setSeqId(uuid);
			authority.setShowName("权威解读");
			authority.setIsColumn(1);
			authority.setIsNav(1);
			authority.setParentId(null);
			authority.setInsertTime(nowTime);
			authority.setOrderNum(4);
			authority.setUpdateCounts(0);
			authority.setUpdateTime(nowTime);
			authority.setUrl("/" + key);
			authority.setDelFlag(0);
			authority.setKey(key);
			addList.add(authority);
			seqIdMap.put(key, uuid);
	
			// 批量插入之前更新文章表的旧数据
			List<Article> allArticle = articleService.selectList(new EntityWrapper<Article>());
			Iterator<Article> articleIterator = allArticle.iterator();
			while(articleIterator.hasNext()){
				Article article = articleIterator.next();
				String oldKey = oldSeqIdMap.get(article.getColumnId());
				String newId = seqIdMap.get(oldKey);
				article.setColumnId(newId);
			}
			
			// 批量插入
			systemMenuService.insertBatch(addList);
			// 批量更新
			if(allArticle != null && allArticle.size() > 0){
				articleService.updateBatchById(allArticle, Contains.BATCH_COUNTS);
			}
			
			// 放入到变量对象中
			EntityWrapper<SystemMenu> getNewMenu = new EntityWrapper<SystemMenu>();
			
			getNewMenu.eq("IS_COLUMN", 1);
			getNewMenu.eq("DEL_FLAG", 0);
			
			getNewMenu.orderBy("ORDER_NUM", true);
			
			List<SystemMenu> columns = systemMenuService.selectList(getNewMenu);
			
			Contains.menuInfo = columns;
			
			Map<String,SystemMenu> menuKeyMap = new HashMap<String,SystemMenu>();
			Iterator<SystemMenu> iterator = columns.iterator();
			while(iterator.hasNext()){
				SystemMenu newMenu = iterator.next();
				menuKeyMap.put(newMenu.getKey(), newMenu);
			}
			Contains.menuKeyMap = menuKeyMap;
			
			return true;
		} catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}

}

